package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.AnswerListAdapter;
import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.listener.IMyAnswerListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.MyAnswerListManager;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 我的回答
 *
 * @author yejianjun
 */
public class MyAnswerActivity extends BaseActivity implements IMyAnswerListListener {
    protected static final String TAG = "MyAnswerActivity";
    private ListView mListView;
    private AnswerListAdapter adapter;
    private MyAnswerListManager manager;
    private List<QaResponse> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.my_answer);
        setTitleName("我的回答");
        init();
        requestData();
    }

    private void requestData() {
        manager = new MyAnswerListManager(this);
        manager.addMyAnsListListener(this);
        manager.reqAnswerList(GlobalContext.getAccountId(), 0, 99);
    }

    private void init() {
        mListView = (ListView) findViewById(R.id.answerListView);
        adapter = new AnswerListAdapter(this);
        mList = new ArrayList<QaResponse>();
        adapter.setData(mList);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(itemClickListener);
    }

    OnItemClickListener itemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //点击问题 跳转到问题详情
            intentDetails(mList.get(position));
        }
    };

    private void intentDetails(QaResponse resp) {
        Utils.intoQaDetailActivity(MyAnswerActivity.this, resp);
    }

    @Override
    public void onMyAnswerListFailure(int errorCode, String errorMsg) {
    }

    @Override
    public void onMyAnswerListSuccess(List<QaResponse> objdata) {
        if (null == objdata || objdata.size() == 0) {
            return;
        }
        mList = objdata;
        adapter.setData(mList);
        adapter.notifyDataSetChanged();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeMyAnsListListener(this);
        }
    }
}
